package org.example.mapper;

import org.apache.ibatis.annotations.*;
import org.example.entity.Dingdan;
import org.example.vo.DingdanVO;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@Mapper
public interface Dingdanmapper {
    /**
     * 新增
     *
     * @param dingdan
     */
    @Insert("INSERT INTO dingdan(product_name, selected_specs," +
            " quantity, price, image, yingfe, address, time, zt,username," +
            "yonghuusername,product_id,fanhuodi) " +
            "VALUES (#{productName}, #{selectedSpecs}," +
            " #{quantity}, #{price}, #{image}, #{yingfe}, #{address}," +
            " #{time}, #{zt},#{username},#{yonghuusername},#{productId},#{fanhuodi})")
    void xz(Dingdan dingdan);

    /**
     * 查询
     *
     * @param username
     * @return
     */
    @Select("select * from dingdan where yonghuusername=#{username} and zt=#{zt}")
    List<Dingdan> cx(String username, int zt);

    /**
     * 修改
     *
     * @param id
     * @param zt
     */
    @Update("UPDATE dingdan\n" +
            "SET zt = #{zt}\n" +
            "WHERE id=#{id}")
    void xg(int id, int zt);

    /**
     * 删除
     *
     * @param id
     */
    @Delete("delete from dingdan where id=#{id}")
    void sc(int id);

    /**
     * 退款修改
     *
     * @param id
     * @param i
     * @param tuih
     */
    @Update("UPDATE dingdan\n" +
            "SET zt = #{i},tuih=#{tuih}\n" +
            "WHERE id=#{id}")
    void xgxz(int id, int i, String tuih);

    /**
     * 数目查询
     *
     * @param username
     * @param i
     * @return
     */
    @Select("select count(*) from dingdan where yonghuusername=#{username} and zt=#{i} ;")
    Integer shucx(String username, int i);

    /**
     * 订单删除
     *
     * @param username
     */
    @Delete("delete from dingdan where  yonghuusername=#{username}")
    void zx(String username);

    /**
     * 商家处理查询
     *
     * @param username
     * @param i
     * @return
     */
    List<Dingdan> glcx(String username, int i);

    /**
     * 注销修改
     *
     * @param username
     */
    @Update("UPDATE dingdan\n" +
            "SET zt = 7\n" +
            "WHERE yonghuusername=#{username}\n" +
            "AND (\n" +
            "    zt=6 OR zt=4 OR zt=3\n" +
            ")\n" +
            "AND username IS NOT NULL")
    void zxxg(String username);

    /**
     * 处理修改
     *
     * @param id
     * @param i
     */
    void clxg(int id, int i);

    @Delete("delete from dingdan where username is null and yonghuusername is null")
    void clsc(int id);

    /**
     * 数据统计
     *
     * @param username
     * @return
     */
    @Select("select  * from dingdan where username=#{username} and( zt=3 or " +
            "zt=4 or zt=6 or zt=7 )")
    List<DingdanVO> sjtjcx(String username);

    /**
     * 数据统计
     *
     * @param username
     * @param todayStart 今天开始时间
     * @param todayEnd   今天结束时间
     * @return
     */
    @Select("SELECT * FROM dingdan WHERE username = #{username} AND time >= " +
            "#{todayStart} AND time <= #{todayEnd} AND (zt = 3 OR zt = 4 OR zt = 6 OR zt = 7)")
    List<DingdanVO> tjcx(String username, LocalDateTime todayStart, LocalDateTime todayEnd);

    /**
     * 数据统计
     *
     * @param username
     * @param todayStart 今天开始时间
     * @param todayEnd   今天结束时间
     * @return
     */
    @Select("SELECT * FROM dingdan WHERE username = #{username} AND time >= " +
            "#{todayStart} AND time <= #{todayEnd} AND (zt = 3 OR zt = 4 OR zt = 6 OR zt = 7)")
    Set<DingdanVO> ctjcx(String username, LocalDateTime todayStart, LocalDateTime todayEnd);

    /**
     * 数据统计
     *
     * @param username
     * @return
     */
    @Select("select  * from dingdan where username=#{username} ")
    List<Dingdan> sycx(String username);

    @Update(" UPDATE dingdan set\n" +
            " username=null where username=#{username}\n")
    void xgzx(String username);


}
